package com.lovo.Atm;

import com.lovo.Stream.SerializeUtil;
import com.lovo.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer extends JPanel implements ActionListener {
    private Home frame;
    private JButton backImg;
    private JButton sureImg;
    private JLabel noteAccount;
    private JLabel noteBlance;
    private JTextField printAcc;
    private JTextField printBla;
    private Icon backBtn = Util.iconImg("/images/buttonOff.jpg");
    private Icon sureBtn = Util.iconImg("/images/buttonSure.jpg");


    public Transfer(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //用户名提示和输入框
        noteAccount = new JLabel("请输入转账用户名：");
        noteAccount.setFont(new Font("宋体", Font.BOLD, 20));
        noteAccount.setBounds(350, 200, 250, 30);
        this.add(noteAccount);

        printAcc = new JTextField();
        printAcc.setBounds(620, 200, 100, 30);
        this.add(printAcc);

        //转账金额
        noteBlance = new JLabel("请输入转账金额：");
        noteBlance.setFont(new Font("宋体", Font.BOLD, 20));
        noteBlance.setBounds(350, 250, 250, 30);
        this.add(noteBlance);

        printBla = new JTextField();
        printBla.setBounds(620, 250, 100, 30);
        this.add(printBla);

        //确认按钮
        sureImg = new JButton(sureBtn);
        sureImg.setBounds(0, 400, 140, 50);
        this.add(sureImg);
        sureImg.addActionListener(this);

        //退出按钮
        backImg = new JButton(backBtn);
        backImg.setBounds(870, 400, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (sureImg == e.getSource()) {

            //得到转账用户名，
            String text = printAcc.getText();
            //本金
//            frame.getLoginUser().getBlance();
            //转账金额
            double transferMoney = 0;
            try {
                //转账金额
                transferMoney = Double.valueOf(printBla.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额不能为空，且仅能为数字");
                printBla.setText("");
                return;
            }
            //建立对象
            JDUtil jd = new JDUtil();
            User loginUser = jd.getById(frame.getLoginUser().getId());
            User otherUser = jd.getByUsername(text);
            //判断用户是否存在于系统中
            if (otherUser == null) {
                JOptionPane.showMessageDialog(this, "查无此人");
                printAcc.setText("");
                return;
            }
            // 判断是否转给自己
            if (loginUser.getName().equals(text)) {
                JOptionPane.showMessageDialog(this, "不能转给自己");
                printAcc.setText("");
                return;
            }
            // 判断余额是否足够
            if (transferMoney > frame.getLoginUser().getBlance()) {
                JOptionPane.showMessageDialog(this, "余额不足");
                printBla.setText("");
                return;
            }
            // 当期账户扣钱
            frame.getFindPanel().set(frame.getLoginUser().getBlance() - transferMoney);
            loginUser.setBlance(loginUser.getBlance() - transferMoney);
            // 对方账户加钱
            otherUser.setBlance(otherUser.getBlance() + transferMoney);
            jd.transfer(loginUser,otherUser);
            JOptionPane.showMessageDialog(this, "转账成功");
            frame.switchPanel("menu");
        } else if (e.getSource() == backBtn) {
            frame.switchPanel("menu");
            printAcc.setText("");
            printBla.setText("");
        }


//        for (int i = 0; i < frame.getUser().size(); i++) {
//            if (text.equals(frame.getUser().get(i).getName()) && !text.equals(frame.getLoginUser().getName())) {
//                if (b > a) {
//                    JOptionPane.showMessageDialog(this, "账户余额不足，请重新输入转账金额！");
//                    printBla.setText("");
//                } else if (a >= b) {
//                    //修改本金
//                    frame.getLoginUser().setBlance(a - b);
//                    frame.getFindPanel().set(a - b);
//                    //修改转账用户余额
//                    frame.getUser().get(i).setBlance(frame.getUser().get(i).getBlance() + b);
//                    JOptionPane.showMessageDialog(this, "转账金额为：" + b + "。转账成功！");
//                    printBla.setText("");
//                    printAcc.setText("");
//                    frame.switchPanel("menu");
//                }
//            }
//        }
//
//        if (!flag) {
//            printBla.setText("");
//            printAcc.setText("");
//            JOptionPane.showMessageDialog(this, "账户输入有误！");
//            frame.switchPanel("menu");
//        }

    }
}

